package com.javajun.endofterm.controller;

import com.javajun.endofterm.entity.SysUser;
import com.javajun.endofterm.service.DomitoryService;
import com.javajun.endofterm.service.ExpressDeliveryService;
import com.javajun.endofterm.service.StudentService;
import com.javajun.endofterm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Xiong Limin
 * Date: 2020-12-01-9:23 下午
 */
@RequestMapping("/page")
@Controller
public class PageController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private DomitoryService domitoryService;

    @Autowired
    private ExpressDeliveryService expressDeliveryService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/welcome")
    private String welcomeView(ModelMap map) {
        // 创建首页数据
        long studentCount = studentService.count();
        long domitoryCount = domitoryService.count();
        long expressCount = expressDeliveryService.count();
        long expressNotPickCount = expressDeliveryService.getNotPickUpCount();
        Map<String, Object> mainData = new HashMap<>();
        mainData.put("studentCount", studentCount);
        mainData.put("domitoryCount", domitoryCount);
        mainData.put("expressCount", expressCount);
        mainData.put("expressNotPickCount", expressNotPickCount);
        map.put("mainData", mainData);
        if (studentService.whetherStudent()) {
            return "redirect:/page/welcome2";
        }
        return "page/welcome-1";
    }

    /**
     * 当前学生用户的 welcome 欢迎页面
     * @param map
     * @return
     */
    @RequestMapping(value = "/welcome2")
    private String welcomToView(ModelMap map) {
        Map<String, Object> studentData = new HashMap<>();
        int domitoryId = studentService.getLodStudentDomitoryId();
        //1.所有快递总数
        int domitoryNotPickUpExpress = expressDeliveryService.getDomitoryNotPickUpExpress(domitoryId);
        studentData.put("notPickUpExpress", domitoryNotPickUpExpress);
        //2.待取快递总数
        int domitoryAllExpress = expressDeliveryService.getDomitoryAllExpress(domitoryId);
        studentData.put("allExpress", domitoryAllExpress);
        //3.剩余电量
        Double power = studentService.getLoadStudentDomitoryPower();
        studentData.put("power", power);
        //设置数据到页面
        map.put("studentData", studentData);
        return "page/welcome-2";
    }

    @RequestMapping("/domitory-view")
    private String domitoryView() {
        return "page/domitory-view";
    }

    @RequestMapping("/powerRecord-view")
    private String powerRecordView() {
        return "page/powerRecord-view";
    }

    @RequestMapping(value = "/powerRecharge-view")
    private String powerRechargeView() {
        return "page/powerRecharge-view";
    }

    @RequestMapping(value = "/expressDelivery-view")
    private String expressDeliveryView() {
        return "page/expressDelivery-view";
    }

    @RequestMapping(value = "/student-view")
    private String studentView() {
        return "page/student-view";
    }

    @RequestMapping(value = "/admin-password")
    private String passwordView(ModelMap map) {
        SysUser sysUser = sysUserService.getLoadSysUser();
        map.put("sysUser", sysUser);
        return "page/admin-password";
    }

    @RequestMapping(value = "/admin-setting")
    private String settingView(ModelMap map) {
        SysUser sysUser = sysUserService.getLoadSysUser();
        map.put("sysUser", sysUser);
        return "page/admin-setting";
    }
}
